package com.app.shiyanbiji.controller;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.google.gson.Gson;
import com.app.shiyanbiji.model.Response;
import com.app.shiyanbiji.model.User;
import com.app.shiyanbiji.service.UserService;
import com.app.shiyanbiji.util.SystemUtil;
import com.github.pagehelper.PageInfo;

@Controller
public class UserController {

	private static Logger logger = Logger.getLogger(UserController.class);
	
	Gson gson = new Gson();

	@Resource
	private UserService userService;
	
	@RequestMapping(value = "getUserBaseInfo")
	@ResponseBody
	public void getUserBaseInfo(HttpServletRequest req, HttpServletResponse resp) {

		String userId = req.getParameter("id");
		String str_pageNum = req.getParameter("pageNum");
        String str_pageSize = req.getParameter("pageSize");
        
        int pageNum = 0, pageSize = 0;
        try {
            pageNum = Integer.parseInt(str_pageNum);
        } catch (NumberFormatException e) {
            return;
        }
        try {
            pageSize = Integer.parseInt(str_pageSize);
        } catch (NumberFormatException e) {
            return;
        }
		
        PageInfo<User> user = userService.selectUserBaseInfoById(pageNum, pageSize);

		Response response = new Response();
		response.setStatus(1);
		response.setMsg("success");
		response.setData(user);

		SystemUtil.writeResponse(req, resp, gson.toJson(response));
	}
	
	@RequestMapping(value = "getUserBaseInfoToJsp.do")
	@ResponseBody
	public void getUserBaseInfoToJsp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String str_pageNum = req.getParameter("pageNum");
        String str_pageSize = req.getParameter("pageSize");
        
        int pageNum = 0, pageSize = 0;
        try {
            pageNum = Integer.parseInt(str_pageNum);
        } catch (NumberFormatException e) {
            req.setAttribute("err", "页码只能是大于0的整数，请重新输入!");
			req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
        try {
            pageSize = Integer.parseInt(str_pageSize);
        } catch (NumberFormatException e) {
            req.setAttribute("err", "页面大小只能是大于0的整数，请重新输入!");
			req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
		
        PageInfo<User> user = userService.selectUserBaseInfoById(pageNum, pageSize);

        req.setAttribute("page", user);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
	}
	
	@RequestMapping(value = "getUserBaseInfoToVelocity.do")
	@ResponseBody
	public ModelAndView getUserBaseInfoToVelocity(HttpServletRequest req, HttpServletResponse resp, ModelMap modelMap) {
		String str_pageNum = req.getParameter("pageNum");
        String str_pageSize = req.getParameter("pageSize");
        
        int pageNum = 0, pageSize = 0;
        try {
            pageNum = Integer.parseInt(str_pageNum);
        } catch (NumberFormatException e) {
        	modelMap.put("err", "页码只能是大于0的整数，请重新输入!");
        	ModelAndView view = new ModelAndView();
    		view.setViewName("/index");
    		view.addAllObjects(modelMap);
    		return view;
        }
        try {
            pageSize = Integer.parseInt(str_pageSize);
        } catch (NumberFormatException e) {
        	modelMap.put("err", "页面大小只能是大于0的整数，请重新输入!");
        	ModelAndView view = new ModelAndView();
    		view.setViewName("/index");
    		view.addAllObjects(modelMap);
    		return view;
        }
		
        PageInfo<User> user = userService.selectUserBaseInfoById(pageNum, pageSize);

        modelMap.put("page", user);
        ModelAndView view = new ModelAndView();
		view.setViewName("/index");
		view.addAllObjects(modelMap);
		return view;
	}
}
